package 第五天

fun main() {
    val list = listOf<String>("n", "m", "h", "q")
    //正序排序
//    println(list.sorted())
    //倒叙
//    println(list.sortedDescending())

    //===========对象排序
    val person = listOf(Human("张三", 20), Human("李四", 120), Human("王五", 10))
    //按照年纪正序排序
    println(person.sortedBy {
        it.age
    })
    //按照年纪到序排序
    println(person.sortedByDescending {
        it.age
    })
    //自定义比较器 kotlin 匿名对象有一个Object关键字
    println(with(person) {
        sortedWith(Comparator<Human> { o1, o2 ->
            val o1Age = o1.age
            val o2Age = o2.age
            o1Age - o2Age
        })
    })
}

class Human(var name: String, var age: Int) {
    override fun toString(): String {
        return "Human ( name = $name  age = $age)"
    }

}